package com.thd.springboottest.sso.client.service;

import com.thd.springboottest.sso.client.vo.Store;
import com.thd.springboottest.sso.vo.AuthBean;

import javax.servlet.ServletResponse;
import java.util.UUID;

/**
 * com.thd.springboottest.sso.client.service.LoginService
 *
 * @author: wanglei62
 * @DATE: 2021/8/22 16:03
 **/
public class LoginService {

    public static String login(AuthBean authBean){
        System.out.println("SSO Client登录自己的系统");
        AuthBean auth = Store.getByUsr(authBean.getUsr());
        if(null != auth){
            return auth.getSessionId();
        }else{
            String sessionId = UUID.randomUUID().toString();
            authBean.setSessionId(sessionId);
            Store.add(authBean);
            return sessionId;
        }
    }

    public static String loginAndSetCookie(AuthBean authBean , ServletResponse response){
        String sessionId = LoginService.login(authBean);
        SessionIdUtil.setSessionId(response,sessionId);
        return sessionId;
    }

}
